package com.hansen.library.ui.widget.textview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

import com.hansen.library.R;
import com.hansen.library.utils.ScreenSizeUtils;

/**
 * textview 添加 drawable，支持设置首行drawable居中
 * Created by han on 2018/10/29 0029.
 */

public class DrawableTextView extends AppCompatTextView {

    private Context mContext;

    private Paint mPaint;

    private String mText;

    private int mWidth, mHeight;
    private int drawableLeftWidth, drawableTopWidth, drawableRightWidth, drawableBottomWidth;
    private int drawableLeftHeight, drawableTopHeight, drawableRightHeight, drawableBottomHeight;

    private boolean drawableAliganCenter;

    public DrawableTextView(Context context) {
        this(context, null);
    }

    public DrawableTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mContext = context;

        if (attrs != null) {
            TypedArray array = mContext.obtainStyledAttributes(attrs, R.styleable.DrawableTextViewStyles);

            drawableLeftWidth = array.getDimensionPixelSize(R.styleable.DrawableTextViewStyles_drawableLeftWidth, 0);
            drawableTopWidth = array.getDimensionPixelSize(R.styleable.DrawableTextViewStyles_drawableTopWidth, 0);
            drawableRightWidth = array.getDimensionPixelSize(R.styleable.DrawableTextViewStyles_drawableRightWidth, 0);
            drawableBottomWidth = array.getDimensionPixelSize(R.styleable.DrawableTextViewStyles_drawableBottomWidth, 0);
            drawableLeftHeight = array.getDimensionPixelSize(R.styleable.DrawableTextViewStyles_drawableLeftHeight, 0);
            drawableTopHeight = array.getDimensionPixelSize(R.styleable.DrawableTextViewStyles_drawableTopHeight, 0);
            drawableRightHeight = array.getDimensionPixelSize(R.styleable.DrawableTextViewStyles_drawableRightHeight, 0);
            drawableBottomHeight = array.getDimensionPixelSize(R.styleable.DrawableTextViewStyles_drawableBottomHeight, 0);

            drawableAliganCenter = array.getBoolean(R.styleable.DrawableTextViewStyles_drawableAliganCenter, false);

            mText = array.getString(R.styleable.DrawableTextViewStyles_drawableText);

            array.recycle();
        }

        initPaint();
    }

    private void initPaint() {
        if (mText != null && mPaint == null) {
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setColor(Color.WHITE);
            mPaint.setTextAlign(Paint.Align.CENTER);
            mPaint.setTextSize(ScreenSizeUtils.sp2Px(mContext, 12));
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable[] drawables = getCompoundDrawables();

        Drawable drawableLeft = drawables[0];
        Drawable drawableTop = drawables[1];
        Drawable drawableRight = drawables[2];
        Drawable drawableBottom = drawables[3];

        if (drawableLeft != null) {
            setDrawable(drawableLeft, 0, drawableLeftWidth, drawableLeftHeight, canvas);
        }
        if (drawableTop != null) {
            setDrawable(drawableTop, 1, drawableTopWidth, drawableTopHeight, canvas);
        }
        if (drawableRight != null) {
            setDrawable(drawableRight, 2, drawableRightWidth, drawableRightHeight, canvas);
        }
        if (drawableBottom != null) {
            setDrawable(drawableBottom, 3, drawableBottomWidth, drawableBottomHeight, canvas);
        }

        super.onDraw(canvas);

        if(drawableLeft != null) {
            if (mPaint != null) {
                Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();

                int baseline = (drawableLeft.getIntrinsicHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top + 1 + getPaddingTop();

                canvas.drawText(mText, drawableLeft.getIntrinsicWidth() / 2, baseline, mPaint);
            }
        }
    }

    private void setDrawable(Drawable drawable, int tag, int drawableWidth, int drawableHeight, Canvas canvas) {
        //获取图片实际长宽
        int width = drawableWidth == 0 ? drawable.getIntrinsicWidth() : drawableWidth;
        int height = drawableHeight == 0 ? drawable.getIntrinsicHeight() : drawableHeight;
        int left = 0, top = 0, right = 0, bottom = 0;

        switch (tag) {
            case 0:
            case 2:
                left = 0;
                top = drawableAliganCenter ? 0 : -getLineCount() * getLineHeight() / 2 + getLineHeight() / 2;
                right = width;
                bottom = top + height;
                break;
            case 1:
                left = drawableAliganCenter ? 0 : -mWidth / 2 + width / 2;
                top = 0;
                right = left + width;
                bottom = top + height;
                break;
        }

        drawable.setBounds(left, top, right, bottom);
    }

    public void setDrawText(String text) {
        if(text != null) {
            this.mText = text;
        }

        initPaint();

        postInvalidate();
    }
}
